package net.cathackers.devel.scmp.xmpp;

import net.cathackers.devel.scmp.factories.SCMPFactory;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * IQ stanzas provide a structured request-response mechanism. like
 * Authentication and roster operations Each IQ stanza has a specific type that
 * indicates his state: request may be of type "set" or "get". response may be
 * result or error Each stanza should have unique id. So Clients will use these
 * ids to identify responses
 */
public class IQPacket extends Packet {
    /**
     * Each IQ stanza has attribute called namespace which indicate the feature
     * that this stanza provided For example the namespace "jabber:iq:auth"
     * provides authentication feature
     */
    protected String namespace_;
    protected String name_;
    protected String type_;
    protected Logger logger_;

    /**
     * Constructs a new IQPacket object
     */
    public IQPacket() {
	super();
	logger_ = SCMPFactory.getLogger(this);
    }

    /**
     * Constructs a new IQPacket with specific type
     * 
     * @param type
     *            may be "set"or "get" or "result" or "error"
     */
    public IQPacket(IQType type) {
	this();
	rootElement_ = codeBehind_.addElement("iq");
	this.setIQType(type);
    }

    /**
     * Constructs a new IQPacket object by parsing DOM object
     * 
     * @param doc
     *            DOM object
     */
    public IQPacket(Document doc) {
	this();
	setDocument(doc);
    }

    /**
     * Constructs a new IQPacket from incoming packet
     * 
     * @param packet
     */
    public IQPacket(Packet packet) {
	this();
	setDocument(packet.getDocument());
	setID(packet.getID());
	setBaseStreamID(packet.getBaseStreamID());
    }

    /**
     * set attributes using attributes of incoming DOM object
     */
    @Override
    public void setDocument(Document doc) {
	super.setDocument(doc);
	type_ = rootElement_.attribute("type").getStringValue();
	if (rootElement_.elements().size() > 0) {
	    Element mainElement = (Element) rootElement_.elements().get(0);
	    name_ = mainElement.getName();
	    namespace_ = mainElement.getNamespace().getStringValue();
	}
    }

    /**
     * set IQPacket type
     * 
     * @param type
     * @see IQType
     */
    public void setIQType(IQType type) {
	rootElement_.addAttribute("type", type.toString());
    }

    /**
     * return the type of IQPacket
     * 
     * @return the IQ type
     * @see IQType
     */
    public IQType getIQType() {
	String type = rootElement_.attributeValue("type");
	if (type != null) {
	    return IQType.valueOf(type);
	} else {
	    return null;
	}
    }

    /**
     * 
     * @return the name of the main element
     */
    public String getName() {
	if (name_ == null) {
	    if (rootElement_.elements().size() > 0) {
		Element mainElement = (Element) rootElement_.elements().get(0);
		name_ = mainElement.getName();

	    }
	}

	return name_;
    }

    /**
     * 
     * @return the namespace of this IQPacket
     */
    public String getNamespace() {
	if (namespace_ == null) {
	    if (rootElement_.elements().size() > 0) {
		Element mainElement = (Element) rootElement_.elements().get(0);
		namespace_ = mainElement.getNamespace().getStringValue();
	    }

	}
	return namespace_;
    }

    /**
     * set error element in this IQPacket
     */
    @Override
    public void setError(StanzaErrors error) {
	super.setError(error);
	setIQType(IQType.error);
    }

}